package com.pengtu.web.project.sub;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.Constants;
import com.pengtu.entity.project.SubentryInfo;
import com.pengtu.entity.project.SubprojectInfo;
import com.pengtu.entity.project.SubunitInfo;
import com.pengtu.service.project.SubManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

public class SubunitAction extends BaseActionSupport<SubunitInfo> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	private String projectId;
	private SubManager submanager;
	private SubunitInfo subunit;

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String delete() throws Exception {
		//查找出将要删除的子单位下的分部信息
		List<SubprojectInfo> subprojectList = submanager.findBySubproject("subunitId", id);
		if(subprojectList.size()>0){
			for (SubprojectInfo subprojectInfo : subprojectList) {
				//查找分项
				List<SubentryInfo> subentryList = submanager.findBySubentryInfo("subprojectId", subprojectInfo.getId());
				if(subentryList.size()>0){
					for (SubentryInfo subentryInfo : subentryList) {
						//删除分项
						submanager.deleteSubentry(subentryInfo.getId());
					}
				}
				//删除分部
				submanager.deleteSubproject(subprojectInfo.getId());
			}
		}
		//删除子单位工程信息
		submanager.deteleSubunit(id);
		Struts2Utils.renderJson(Constants.EXT_SUCCESS_JSON_STR);
		return NONE;
	}

	/* 图纸合同段子单位工程操作 */
	/**
	 * 
	 * getSubunit: 适用:查找子单位工程信息
	 * 
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public String getSubunit() {
		SubunitInfo subunit = submanager.getSubunit("id", id);
		Struts2Utils.renderJsontoExtOneData(subunit);
		return NONE;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			subunit = submanager.getSubunit("id", id);
		} else {
			subunit = new SubunitInfo();
		}
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isEmpty(id)){
			String contractId = Struts2Utils.getParameter("contractId");
			subunit.setDrawingsContractId(contractId);
		}
		submanager.saveSubunit(subunit);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"工程分组!");
		Struts2Utils.renderJson(Constants.EXT_SUCCESS_JSON_STR);
		return NONE;
	}

	@Override
	public SubunitInfo getModel() {
		// TODO Auto-generated method stub
		return subunit;
	}


	@Autowired
	public void setSubmanager(SubManager submanager) {
		this.submanager = submanager;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setSubunit(SubunitInfo subunit) {
		this.subunit = subunit;
	}

}
